<template>
  <div class="user-top">
    <el-form label-width="120px" ref="formRef" :model="formData">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="部门名称" prop="name">
            <el-input placeholder="请输入部门名称" v-model="formData.name" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="部门编号" prop="depNo">
            <el-input placeholder="请输入部门编号" v-model="formData.depNo" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="创建时间" prop="createtime">
            <el-date-picker
              v-model="formData.createtime"
              type="datetimerange"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              style="width: 100%"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <footer>
      <el-button type="warning" icon="Refresh" @click="resetForm">重置</el-button>
      <el-button type="primary" icon="Search" @click="handleSearchQuery">查询</el-button>
    </footer>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { ElForm } from 'element-plus'

const emit = defineEmits(['handleSearchQuery', 'resetAndQueryList'])

// 获取表单的类型并通过InstanceType转换为可使用的类型
const formRef = ref<InstanceType<typeof ElForm>>()

const formData = reactive({
  name: '',
  depNo: '',
  createtime: ''
})

/**
 * 重置表单内容
 */
const resetForm = () => {
  // 当表单数据过多的时候需要清除可以使用表单提供的实例方法进行清除
  formRef.value?.resetFields()
  // 清空表单数据的时候，重新获取所有表单数据
  emit('resetAndQueryList')
}
const handleSearchQuery = () => {
  emit('handleSearchQuery', formData)
}
</script>

<style lang="scss" scoped>
.user-top {
  width: 100%;
  footer {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
